package com.zhuss.core.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.orign.CreateOrignBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.OrignDao;
import com.zhuss.core.db.entity.Orign;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.StartStopEnum;
import com.zhuss.exception.AabException;
import com.zhuss.util.JsonUtil;
import com.zhuss.util.PingYinUtil;

/**
 * 创建orign processor
 * @Author: zhushanshuo
 * @Date: 2020/02/20 21:55
 */
@Component("orignCreate")
public class OrignCreate extends AbstractProcessor<Object, CreateOrignBo> implements Processor<Object, CreateOrignBo> {

    @Autowired
    private OrignDao orignDao;
    
    @Autowired
	@Qualifier("orignRead")
	private OrignRead orignRead;
    
    /**
     * 校验
     */
    @Override
    public void check(CreateOrignBo bo) throws AabException {
    	
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(CreateOrignBo param) {
    	
    	Orign record = new Orign();
    	record.setType(param.getType());
    	record.setValue(param.getValue());
    	record.setData(param.getData());
    	record.setRemoveIdList(param.getRemoveIdList());
    	record.setStatus(param.getStatus() == null ? StartStopEnum.启用.key() : param.getStatus());
    	record.setSort(param.getSort());
    	
    	record.setParent(param.getParent());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	orignDao.create(record);
    	param.setTargetId(record.getOrignId());
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(CreateOrignBo param) throws AabException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.orign创建.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getType() + ":" + param.getValue());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
